package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.PhyUserMapper;
import com.gxa.jjty.admin.service.PhyUserService;
import com.gxa.jjty.core.pojo.PhyUser;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.List;

/**
 * @ClassName PhyUserServiceImpl
 * @Author: ws
 * @date 2021/12/2 14:21
 */
@Service("phyUserService")
public class PhyUserServiceImpl extends BaseServiceImpl<PhyUser> implements PhyUserService {

    @Autowired
    PhyUserMapper phyUserMapper;

    @Override
    public void resetTodayOrderSum() {
        phyUserMapper.resetTodayOrderSum();
    }

    @Override
    public PageInfo<PhyUser> selectByTimeAnd(Integer page, Integer size, Timestamp from, Timestamp to, PhyUser phyUser) throws IllegalAccessException {
        PageHelper.startPage(page,size);
        List<PhyUser> ts = null;
        Example example=null;

        // 不带任何条件
        if (phyUser == null && from == null && to == null){
            ts = phyUserMapper.selectAll();
        }

        // 带条件
        example = new Example(phyUser.getClass());
        Example.Criteria criteria = example.createCriteria();
        Class<?> cls =phyUser.getClass();
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            String name = declaredField.getName();
            Object o = declaredField.get(phyUser);
            if (o != null){
                criteria.andLike(name,"%"+o.toString()+"%");
            }
        }
        if (from != null && to != null) {
            criteria.andBetween("regTime",from, to);
        }
        //实现逆序
        example.orderBy(declaredFields[0].getName()).desc();
        ts = phyUserMapper.selectByExample(example);


        PageInfo<PhyUser> list = PageInfo.of(ts);
        return list;
    }

}
